package com.lgj.springboot_lgj.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lgj.springboot_lgj.bean.basic.Carousel;
import com.lgj.springboot_lgj.dao.basic.CarouselMapper;
import com.lgj.springboot_lgj.dao.ext.BaseCarouselExtMapper;
import com.lgj.springboot_lgj.exception.CustomerException;
import com.lgj.springboot_lgj.service.ICarouselService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
//service层接口的实现类
// 第三次测试
@Service
public class ICarouselServiceImpl implements ICarouselService {
    @Autowired
    private CarouselMapper mapper;
    @Autowired
    private BaseCarouselExtMapper extMapper;
    @Override
    public PageInfo<Carousel> findAll(int pageNum, int pageSize) {
        //分页查询
        PageHelper.startPage(pageNum,pageSize);
        List<Carousel> list = extMapper.selectAll();
        PageInfo<Carousel> carouselPageInfo = new PageInfo<>(list);
        return carouselPageInfo;

    }
    @Override
    public void deleteById(int id) {
       // 轮播图正在启用中不能删除
        Carousel carousel = mapper.selectByPrimaryKey(id);
        if (carousel == null) {
            throw new CustomerException("该轮播图不存在");
        }
        if (carousel.getCarouselStatus() == 1) {
            throw new CustomerException("该轮播图正在启用中，无法删除");
        }
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public void saveCarousel(Carousel carousel) {
        //当添加的轮播图url重复时，提示无法添加
        Carousel urlRepeat = extMapper.selectUrl(carousel.getCarouselUrl());
        if(urlRepeat != null){
            throw new CustomerException("添加的轮播图重复，无法添加");
        }
        //当添加轮播图的名称重复时，提示用户不能添加
        Carousel nameRepeat = extMapper.selectName(carousel.getCarouselName());
        if(nameRepeat != null){
            throw new CustomerException("添加的名字重复，无法添加");
        }
        carousel.setCarouselStatus(0);  //默认状态为0
        mapper.insert(carousel);

    }

    @Override
    public void updateCarousel(Carousel carousel) {
        List<Carousel> list = extMapper.selectCarouselWithYse();
        //不能禁用所有轮播图
        /*if (carousel.getCarouselStatus() == 0 && list.size() == 1 && carousel.getCarouselName() == null) {
            throw new CustomerException("至少启用一个轮播图");
        }
        mapper.updateByPrimaryKeySelective(carousel);*/
        //判断只有一个启动轮播图的id与要禁用的id是否相等

        int num = carousel.getCarouselId();
        String name = carousel.getCarouselName();
        if (list.size() == 1){
            int i = list.get(0).getCarouselId();
            if(i == num){
                if(name != null){
                    mapper.updateByPrimaryKeySelective(carousel);
                }else {
                 throw new CustomerException("至少启用一个轮播图");
                }
            }
        }

        //当更改轮播图的链接重复时，提示用户不能更改
        Carousel urlRepeat = extMapper.selectUrl(carousel.getCarouselUrl());
        if(urlRepeat != null && (urlRepeat.getCarouselId() != carousel.getCarouselId())){
            throw new CustomerException("更改的轮播图重复，无法更改");
        }
        //当更改轮播图的名称重复时，提示用户不能更改
        Carousel nameRepeat = extMapper.selectName(carousel.getCarouselName());
        if(nameRepeat != null && (nameRepeat.getCarouselId() != carousel.getCarouselId())){
            throw new CustomerException("更改的名称重复，无法更改");
        }
        mapper.updateByPrimaryKeySelective(carousel);
    }

    @Override
    //查找已启用的轮播图
    public List<Carousel> findYesCarousel() {
        List<Carousel> list = extMapper.selectCarouselWithYse();
        return list;
    }
    @Override
    //查询轮播图
    public  List<Carousel> findOneByName(String name) {
        List<Carousel> list = extMapper.selectAnyName(name);
        if (list == null){
            throw new CustomerException("没有该轮播图");
        }
        return list;
    }
}
